package util;

import cn.hutool.core.io.file.FileReader;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import pojo.ConfigPoJo;
import pojo.RequestPoJo;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.*;

public class DataCleaningUtil {


    public static void main(String[] args) throws Exception {
        save();
    }

    /**
     * 遍历全部.db文件，将结果添加到resultMap，并返回已处理的文件路径
     * @param fileList 待遍历文件
     * @return Set<String> .db文件路径集合
     */
    public static Set<String> readLocalDB(List<File> fileList, Map<String, Set<RequestPoJo>> resultMap) {
        Set<String> pathSet = new LinkedHashSet<>();

        List<String> strList = null;
        FileReader fileReader = null;
        Set<RequestPoJo> set = null;
        JSONArray jsonArray = null;
        if (!CollectionUtils.isEmpty(fileList)) {
            for (File file : fileList) {
                if (file.getName().endsWith(".db")) {
                    pathSet.add(file.getPath());

                    fileReader = new FileReader(file.getPath());
                    strList = fileReader.readLines();
                    if (!CollectionUtils.isEmpty(strList)) {
                        for (int i = 0; i < strList.size(); i+=2) {
                            String key = strList.get(i).trim();
                            String value = strList.get(i+1);
                            jsonArray = JSONArray.parseArray(value);
                            set = new LinkedHashSet();
                            for (Object json : jsonArray) {
                                set.add(new RequestPoJo( ((JSONObject)json).getJSONObject("json")));
                            }
                            if (resultMap.get(key) == null) {
                                resultMap.put(key,set);
                            } else {
                                resultMap.get(key).addAll(set);
                            }
                        }
                    }
                }
            }
        }

        return pathSet;
    }

    public static Map<String, Set<RequestPoJo>> save() throws Exception {
        // 预设返回值
        Map<String, Set<RequestPoJo>> resultMap = new LinkedHashMap<>();

        // 获取配置
        ConfigPoJo configPoJo = ConfigUtil.getConfig();
        // 递归获取根目录下全部.log文件和.db文件
        List<File> fileList = FileUtil.getLogFile(configPoJo.getLogSavePath(), null);
        // 预设变量内存回收
        JSONArray array = null;
        FileReader fileReader = null;
        List<String> list = null;
        JSONObject json = null;
        JSONObject sessionJson;
        // 遍历全部.db文件，将结果添加到resultMap，并返回已处理的文件路径

        Set<String> oldLog = readLocalDB(fileList, resultMap);

        Map<String, Set<RequestPoJo>> tempLogMap = null;

        if (!CollectionUtils.isEmpty(fileList)) {
            for (File file : fileList) {
                // 读取下载后的日志文件并过滤旧的
                if (file.getName().endsWith(".log") && !oldLog.contains(file.getPath()+".db")) {
                    System.out.println(DateUtil.now() + "  --  " + file.getName() + "  -- 文件开始解析");

                    tempLogMap = new LinkedHashMap<>();
                    long start = System.currentTimeMillis();
                    fileReader = new FileReader(file.getPath());
                    list = fileReader.readLines();
                    int total = 0;
                    for (String str : list) {
                        // 将一行日志转换为json数组
                        array = JSONFormatUtil.parseJSONArrsy(str);
                        if (array.size() > 0) {
                            total++;
                        }
                        for (Object obj : array) {
                            json = (JSONObject) obj;
                            if (json.containsKey("sessionContext")) {
                                sessionJson = json.getJSONObject("sessionContext");
                                if (sessionJson.containsKey("serviceCode") && !StringUtils.isEmpty(sessionJson.getString("serviceCode"))) {
                                    String serviceCode = sessionJson.getString("serviceCode");
                                    // 将结果添加resultMap
                                    if (resultMap.get(serviceCode) == null) {
                                        resultMap.put(serviceCode, new LinkedHashSet<>());
                                    }
                                    resultMap.get(serviceCode).add(new RequestPoJo(json));

                                    // 将结果添加tempLogMap
                                    if (tempLogMap.get(serviceCode) == null) {
                                        tempLogMap.put(serviceCode, new LinkedHashSet<>());
                                    }
                                    tempLogMap.get(serviceCode).add(new RequestPoJo(json));
                                }
                            }
                        }
                    }
                    long time = System.currentTimeMillis() - start;
                    System.out.println(DateUtil.now() + "  --  " + file.getName() + "  -- 文件解析完成,耗时" + time + "毫秒,总共解析json个数 = " + total);
                    // 保存解析后的文件
                    saveDBFile(tempLogMap, file.getPath()+".db");
                }
            }
        }
        System.out.println(DateUtil.now() + "  --  采集接口编号总数 = " + resultMap.size()) ;
        return resultMap;
    }

    /**
     * 保存解析后的文件，下次不再重复解析
     * @param logMap
     * @param savePath
     * @throws Exception
     */
    public static void saveDBFile(Map<String, Set<RequestPoJo>> logMap, String savePath) throws Exception {
        File file = new File(savePath);
        if (file.exists()) {
            file.delete();
        }
        // 保存到本地文件
        PrintWriter printWriter = new PrintWriter(new FileOutputStream(file));
        for (Map.Entry<String, Set<RequestPoJo>> entry : logMap.entrySet()) {
            printWriter.println(entry.getKey());
            printWriter.println(JSONArray.toJSONString(entry.getValue()));
        }
        printWriter.close();
    }

}
